/**
 * 
 */
package com.example.radioclub.login;

import com.example.radioclub.data.Constants;
import com.example.radioclub.login.yahoo.interfaces.IYahooUserInfoTask;
import com.example.radioclub.tasks.http.HttpGetResponseDownloader;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

/**
 * @author wizard
 * 
 */
public class YahooLoginActivity extends Activity implements IYahooUserInfoTask {
	private final String TAG = Constants.APP_TAG + getClass().getName();
	private HttpGetResponseDownloader httpDownloader;
	private ProgressDialog progressDialog;
	private AccountManager accountManager;
	private Account[] accounts;
	private Account account;
	private String chosenEmail = null;
	private AlertDialog alertDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i(TAG, "Logging in with Yahoo Account.");
		if(Constants.CLIENT_LOGIN_STATUS == false) {
			this.accountManager = AccountManager.get(this);
			this.accounts = this.accountManager.getAccountsByType("com.yahoo");
		}
		
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(data != null) {
			String oauth_token = data.getExtras().getString("oauth_token");
			String oauth_verifier = data.getExtras().getString("oauth_verifier");
			Log.i(TAG, "OAuth Token = " + oauth_token + " & OAuth Verifier = " + oauth_verifier);
		}
		// super.onActivityResult(requestCode, resultCode, data);
	}
	

	@Override
	public void onRequestTokenAcquired(String result) {
		// TODO Auto-generated method stub
		if(this.progressDialog.isShowing()) this.progressDialog.dismiss();
		if(result != null) {
			try {
				String uri = result;
				startActivity(new Intent("android.intent.action.VIEW", Uri.parse(uri)));
			} catch (Exception e) {
				e.printStackTrace();
				Log.i(TAG, e.getMessage());
			}
			
			
			
/*			Log.i(TAG, "URL request token = " + result);
			this.authTokenTask = new YahooAuthTokenTask(this.consumer, this);
			this.authTokenTask.execute(this.provider);
			this.progressDialog = ProgressDialog.show(this, "Please wait.", "Loading.");
						
			WebView web = new WebView(this);
			web.loadUrl(result);
			
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("Authorize App").setView(web).create().show();
*/			
		}
	}


	@Override
	public void onAuthAccessTokenAcquired(String[] result) {
		// TODO Auto-generated method stub
		if(this.progressDialog.isShowing()) this.progressDialog.dismiss();
		if(result != null) {
			Log.i(TAG, "Token = " + result[0] + " and Secret Token = " + result[1]);
		}
	}
}
